﻿package com.sgcc.nfc.lock.presentation.users

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.FragmentUserManagementBinding
import com.sgcc.nfc.lock.domain.model.UserProfile
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class UserManagementFragment : Fragment() {

    private var _binding: FragmentUserManagementBinding? = null
    private val binding get() = _binding!!
    private val viewModel: UsersViewModel by viewModels()
    private val adapter = UserAdapter(::onUserClicked, ::onResetPassword, ::onDeleteUser)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentUserManagementBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.toolbar.title = getString(R.string.user_manage_title)
        binding.userRecycler.apply {
            layoutManager = LinearLayoutManager(requireContext())
            this.adapter = this@UserManagementFragment.adapter
        }
        binding.swipeRefresh.setOnRefreshListener { viewModel.loadUsers() }
        binding.addUserFab.setOnClickListener {
            findNavController().navigate(R.id.userFormFragment)
        }
        observeViewModel()
    }

    private fun observeViewModel() {
        viewModel.users.observe(viewLifecycleOwner) { users ->
            adapter.submitList(users)
            binding.emptyView.isVisible = users.isEmpty()
            binding.swipeRefresh.isRefreshing = false
        }
        viewModel.message.observe(viewLifecycleOwner) { message ->
            message?.let {
                Snackbar.make(binding.root, it, Snackbar.LENGTH_LONG).show()
                viewModel.clearMessage()
            }
            binding.swipeRefresh.isRefreshing = false
        }
    }

    private fun onUserClicked(user: UserProfile) {
        val bundle = Bundle().apply {
            putLong("userId", user.userId)
            putString("username", user.username)
            putString("realName", user.realName)
            putString("phone", user.phone)
            putInt("roleType", user.roleType)
            user.regionId?.let { putLong("regionId", it) }
        }
        findNavController().navigate(R.id.userFormFragment, bundle)
    }

    private fun onResetPassword(user: UserProfile) {
        viewModel.resetPassword(user.userId)
    }

    private fun onDeleteUser(user: UserProfile) {
        viewModel.deleteUser(user.userId)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.swipeRefresh.setOnRefreshListener(null)
        _binding = null
    }
}
